package com.wdzg.member.${modular}.${modelName}.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wdzg.member.util.ResultUtil;
import com.wdzg.member.${modular}.${modelName}.service.${className}Service;
import com.wdzg.member.wonder.utils.JsonUtil;
import com.wdzg.member.${modular}.${modelName}.vo.${className};

/**
 * 
 * @author generate
 */
@RestController
@RequestMapping("${visitUrl}")
@Api("${title}")
@Slf4j
public class ${className}Controller {
	@Autowired
	private ${className}Service ${className?uncap_first}Service; 

	/**
	 * 获取${title}列表
	 * @param page
	 * @param limit
	 * @param ${className?uncap_first}
	 * @return
	 */
	@RequestMapping("get${className}List")
	@ApiOperation(value = "获取${title}列表", response=${className}.class)
	public ResultUtil get${className}List(Integer page, Integer limit, ${className} ${className?uncap_first}){
	    ConcurrentHashMap<String,Object> map = new ConcurrentHashMap<>();
	    map.put("page",page);
        map.put("limit",limit);
	    List<${className}> ${className?uncap_first}s = ${className?uncap_first}Service.get${className}List(map, ${className?uncap_first});
		return ResultUtil.build(0, "", ${className?uncap_first}Service.getCountByExample(${className?uncap_first}), ${className?uncap_first}s);
	}

	/**
	 * 新增${title}
	 * @param ${className?uncap_first}
	 * @return
	 */
	@PostMapping("add${className}")
    @ApiOperation(value = "新增${title}", response = ${className}.class)
    public ModelAndView add${className}(${className} ${className?uncap_first}){
		try {
			${className}Service.saveOrUpdate(${className?uncap_first});
		} catch (Exception e) {
			log.info(e.getMessage()+"/add");
		}
		return "${modular}/${modelName}/${modelName}List";
	}

	/**
	 * 根据${title}id删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据${title}id删除", notes = "根据${title}id删除", response=${className}.class)
	@PostMapping(value = "remove${className}")
	public ResultUtil remove${className}(Long id) {
		String msg = "${title}删除成功!";
		try {
			//查询该${title}是否已经被使用，不能删除
			${className?uncap_first}Service.delete(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			msg = "${title}删除失败";
			return ResultUtil.build(-1, msg);
		}
		return ResultUtil.ok(msg);
	}
}
